package com.tengju.bff.interfaces.manage.platform.message;

import com.tengju.bff.interfaces.manage.platform.message.facade.MessageFacade;
import com.tengju.bff.interfaces.manage.platform.message.vo.*;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.support.application.message.MessageService;
import com.tengju.support.domain.model.message.MessageChannel;
import com.tengju.support.domain.model.message.MessageContentType;
import com.tengju.support.domain.model.message.MessageType;
import com.tengju.support.domain.model.message.OrientationType;
import com.tengju.user.domain.model.user.Staff;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author: 李大仙
 * @date: 2021/6/7 2:13 下午
 */
@RestController
@Api(tags = "平台运营-消息管理")
@RequestMapping(value = "manage/platform/message/")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MessageController {

    private final MessageService messageService;
    private final MessageFacade messageFacade;

    @RequiresRoles("sso")
    @ApiOperation(value = "添加消息", notes = "添加消息")
    @RequestMapping(value = "add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> add(@Valid @RequestBody AddMessageForm form) {
        Staff staff = ManageUtil.getOperateUser();
        messageService.add(form.getTitle(),
                MessageChannel.STATION_LETTER,
                form.getMessageType(),
                form.getContentType(),
                form.getImgUrl(),
                form.getContent(),
                form.getJumpUrl(),
                form.getOrientationType(),
                form.getOrientationObjectList(),
                form.isPush(),
                form.getSendTime(),
                staff.getName());
        return ApiResponse.newSuccess();
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "修改消息", notes = "修改消息")
    @RequestMapping(value = "edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> edit(@Valid @RequestBody EditMessageForm form) {
        Staff staff = ManageUtil.getOperateUser();
        messageService.edit(form.getMessageId(),
                form.getTitle(),
                MessageType.fromVal(form.getMessageType()),
                MessageContentType.fromVal(form.getContentType()),
                form.getImgUrl(),
                form.getContent(),
                form.getJumpUrl(),
                OrientationType.fromVal(form.getOrientationType()),
                form.getOrientationObjectList(),
                form.isPush(),
                form.getSendTime(),
                staff.getName());
        return ApiResponse.newSuccess();
    }


    @RequiresRoles("sso")
    @ApiOperation(value = "删除消息", notes = "删除消息")
    @RequestMapping(value = "remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> remove(@Valid @RequestBody DelMessageForm form) {
        Staff staff = ManageUtil.getOperateUser();
        messageService.remove(form.getMessageId(), staff.getName());
        return ApiResponse.newSuccess();
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "查询消息明细", notes = "查询消息明细")
    @RequestMapping(value = "detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<MessageContentVO> detail(@NotNull(message = "消息ID不能为空") Long messageId) {
        return ApiResponse.newSuccess(messageFacade.detail(messageId));
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "分页查询消息", notes = "分页查询消息")
    @RequestMapping(value = "getByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> getByPage(@Valid GetMessageForm form) {
        return ApiResponse.newSuccess(messageFacade.getByPage(form));
    }

    @ApiOperation(value = "获取站内信分类列表", notes = "获取站内信分类列表")
    @GetMapping(value = "getStationLetterTypeList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getStationLetterTypeList() {
        return ApiResponse.newSuccess(messageFacade.getStationLetterTypeList());
    }

    @ApiOperation(value = "获取消息展现形式列表", notes = "获取消息展现形式列表")
    @GetMapping(value = "getMessageContentTypeList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getMessageContentTypeList() {
        return ApiResponse.newSuccess(messageFacade.getMessageContentTypeList());
    }

    @ApiOperation(value = "获取消息发送状态列表", notes = "获取消息发送状态列表")
    @GetMapping(value = "getMessageSendStatusList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getMessageSendStatusList() {
        return ApiResponse.newSuccess(messageFacade.getMessageSendStatusList());
    }

    @ApiOperation(value = "获取消息定向人群列表", notes = "获取消息定向人群列表")
    @GetMapping(value = "getMessageUserOrientationList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getMessageUserOrientationList() {
        return ApiResponse.newSuccess(messageFacade.getMessageUserOrientationList());
    }
}
